package com.xiguaji.tiktok.v1.ins.product.repository.impl;

import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.product.dto.InsProductBloggerData;
import com.xiguaji.tiktok.v1.ins.product.dto.InsProductData;
import com.xiguaji.tiktok.v1.ins.product.dto.InsProductMedia;
import com.xiguaji.tiktok.v1.ins.product.repository.InsProductExtRepository;
import io.micrometer.core.instrument.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class InsProductExtRepositoryImpl implements InsProductExtRepository {

    @Override
    public Page<InsProductBloggerData> getProductMediaBlogger(long productId, int page, int pageSize,long startTime,long endTime) {
        String selectSql = "select * ";
        String fromSql = " from (select count(*) posts,b.username,b.full_name,b.pic_url,b.is_verified,b.follower_count,b.overall_category_name,b.category_name,sum(m.like_count+m.comment_count) engagements "+
                "from ins_rel_media_product mp left join ins_blogger_main b on mp.uid = b.uid left join ins_media m on mp.shortcode = m.shortcode " +
                "where mp.product_id = ? and b.follower_count > 0 and mp.taken_time between ? and ? group by mp.uid ) mc order by engagements desc";
        return JdbcTemplateHelper.paginate(InsProductBloggerData.class,page,pageSize,selectSql,fromSql,productId,startTime,endTime);
    }

    @Override
    public Page<InsProductMedia> getProductMediaData(long productId,Long uid, int page, int pageSize,Integer mediaType,long startTime,long endTime) {
        String selectSql = "select b.username,b.full_name,b.pic_url,b.is_verified,m.shortcode,m.display_url,m.is_sponsored," +
                "m.like_count+m.comment_count engagements,m.id,m.mid,m.score,m.like_count,m.comment_count,m.er," +
                "case  when m.is_video = 0 THEN 1 when m.product_type = 'feed' THEN 2 when m.product_type = 'igtv' THEN 3 when m.product_type = 'clips'  THEN 4 ELSE 1 end mediaType ";
        String fromSql = "from ins_rel_media_product mp left join ins_blogger_main b on mp.uid = b.uid left join ins_media m on mp.shortcode = m.shortcode " +
                "where mp.product_id = ? and b.follower_count > 0 and mp.taken_time between ? and ? ";

        if(null != mediaType){
            switch (mediaType){
                case 1:
                    fromSql += " and m.is_video = 0 ";
                    break;
                case 2:
                    fromSql += " and m.product_type = 'feed' ";
                    break;
                case 3:
                    fromSql+= " and m.product_type = 'igtv' ";
                    break;
                case 4:
                    fromSql+= " and m.product_type = 'clips' ";
                    break;
                default:
            }
        }

        List<Object> params = new ArrayList<>();
        params.add(productId);
        params.add(startTime);
        params.add(endTime);

        if(null != uid){
            fromSql += "and mp.uid = ?";
            params.add(uid);
        }

        fromSql += " order by engagements desc";
        return JdbcTemplateHelper.paginate(InsProductMedia.class,page,pageSize,selectSql,fromSql,params.toArray());
    }


    @Override
    public Page<InsProductData> getProductDataByUid(long uid, int page, int pageSize,long startTime,long endTime,String category){
        StringBuilder select = new StringBuilder("select a.* ");
        StringBuilder where = new StringBuilder(" from (select count(*) media_count,AVG(m.er) avg_er,SUM(m.comment_count+m.like_count) tec,p.product_name,p.image,p.price,p.overall_category_name,p.product_id  " +
                "from ins_rel_media_product mp left join ins_media m on mp.shortcode = m.shortcode " +
                "left join ins_product p on mp.product_id = p.product_id where mp.uid = ? and mp.taken_time between ? and ? ");
        List<Object> params = new ArrayList<>();
        params.add(uid);
        params.add(startTime);
        params.add(endTime);
        if(StringUtils.isNotEmpty(category)){
            where.append("and p.overall_category_name=? ");
            params.add(category);
        }
        where.append("group by mp.product_id) a order by a.tec desc");

        return JdbcTemplateHelper.paginate(InsProductData.class,page,pageSize,select.toString(),where.toString(),params.toArray());
    }

    @Override
    public Page<InsProductData> getPageProductDataByShortCode(String shortCode, int page, int pageSize) {
        String selectSql = "select p.*,b.username,b.full_name,b.is_verified,b.pic_url ";
        String fromSql = " from ins_rel_media_product mp inner join ins_product p on mp.product_id = p.product_id left join ins_blogger_main b on p.merchant_uid = b.uid where mp.shortcode = ? order by mp.er desc";
        return JdbcTemplateHelper.paginate(InsProductData.class,page,pageSize,selectSql,fromSql,shortCode);
    }
}
